package com.firewolf.vue.service.impl;

import com.firewolf.vue.mapper.UserMapper;
import com.firewolf.vue.mapper.UserRoleMapper;
import com.firewolf.vue.params.UserParam;
import com.firewolf.vue.pojo.User;
import com.firewolf.vue.pojo.UserExample;
import com.firewolf.vue.pojo.UserRole;
import com.firewolf.vue.pojo.UserRoleExample;
import com.firewolf.vue.service.UserService;
import com.firewolf.vue.vo.PageVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 作者：刘兴
 * 时间:19/2/28
 **/
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public PageVo<User> findUserList(UserParam param) {

        PageHelper.startPage(param.getCpage(), param.getPageSize());

        List<User> uList = userMapper.findByParams(param);

        PageInfo<User> pageInfo = new PageInfo<>(uList);
        PageVo<User> vo = new PageVo<>(pageInfo.getTotal(), pageInfo.getList());

        return vo;
    }

    @Override
    public void save(User u) {
        if (u.getId() != null) {
            u.setLastUpdateTime(new Date());
            userMapper.updateByPrimaryKeySelective(u);
            UserRoleExample ure = new UserRoleExample();
            ure.createCriteria().andUserIdEqualTo(u.getId());
            userRoleMapper.deleteByExample(ure);
        } else {
            u.setStatus((byte) 1);
            u.setCreateTime(new Date());
            u.setLastUpdateTime(new Date());
            u.setPassword(new BCryptPasswordEncoder().encode(u.getPassword()));
//            u.setCreateBy();
//            u.setLastUpdateBy();
//            u.setSalt();
            u.setDelFlag((byte) 0);
            userMapper.insert(u);

        }

        //新增中间表
        //Mybtis批量新增
//        insert into tb (a,b) values (),(),()

        String roleids = u.getRoleids();
        if (roleids != null) {
            String[] split = roleids.split(",");
            for (String s : split) {
                UserRole userRole = new UserRole();
                userRole.setRoleId(Long.parseLong(s));
                userRole.setUserId(u.getId());
//                userRole.setCreateBy();
//                userRole.setLastUpdateBy();
                userRole.setCreateTime(new Date());
                userRole.setLastUpdateTime(new Date());
                userRoleMapper.insert(userRole);
            }
        }

    }

    @Override
    public List<User> findAllUsers() {
        return userMapper.selectByExample(null);
    }

    @Override
    public User findUserByName(String username) {
        UserExample ue = new UserExample();
        ue.createCriteria().andNameEqualTo(username);

        List<User> users = userMapper.selectByExample(ue);
        return CollectionUtils.isEmpty(users) ? null : users.get(0);
    }
}


